<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2017-03-17
 * Time: 15:37
 */

namespace app\src\sunsun\cp1000\action;

use app\src\sunsun\common\helper\SnHelper;
use app\src\sunsun\cp1000\logic\Cp1000DeviceLogic;
use GatewayClient\Gateway;
use sunsun\cp1000\req\Cp1000DeviceUpdateReq;


/**
 * Class Cp1000DeviceInfoAction
 * tcp 客户端通用操作
 * @package app\src\sunsun\cp1000
 */
class Cp1000ClientAction extends Cp1000BaseAction
{


    public function updateSession($did, $updateSession)
    {

        $client_id = Gateway::getClientIdByUid($did);
        if (count($client_id) > 0) {
            return Gateway::updateSession($client_id[0], $updateSession);
        }

        return null;
    }

    public function sendMessage($did, $message)
    {
        return $this->sendReqToClient($did, $message, new Cp1000DeviceLogic());
    }

    public function getSession($did)
    {
        $client_id = Gateway::getClientIdByUid($did);
        if (count($client_id) > 0) {
            return Gateway::getSession($client_id[0]);
        }

        return null;
    }


    public function update($did, $url, $len = 0)
    {
        $req = new Cp1000DeviceUpdateReq();
        $req->setUrl($url);
        $req->setSn(SnHelper::getSn());
        $req->setLen($len);
        return $this->sendReqToCp1000Client($did, $req);
    }
}